﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MyEmulators2
{
    partial class Conf_Options_New : ContentPanel
    {
        bool updateGeneral = false;
        bool updateLayout = false;
        bool updateStopEmu = false;
        bool updateGoodmerge = false;
        bool updateDatabase = false;
        bool updateAdvanced = false;

        public Conf_Options_New()
        {
            InitializeComponent();
        }

        void Conf_Options_New_Load(object sender, EventArgs e)
        {
            getOptions();
            keyMapLabel.PreviewKeyDown += new PreviewKeyDownEventHandler(mapKeyDown);
            addEventHandlers();
        }

        #region Event Handlers

        void addEventHandlers()
        {
            //General options
            shownnameBox.TextChanged += new EventHandler(generalOptionsChanged);
            languageBox.TextChanged += new EventHandler(generalOptionsChanged);
            favouriteslaunchBox.CheckedChanged += new EventHandler(generalOptionsChanged);
            onlyShowPCGamesBox.CheckedChanged += new EventHandler(generalOptionsChanged);
            hidelabeldecorationsBox.CheckedChanged += new EventHandler(generalOptionsChanged);
            autoconfemuBox.CheckedChanged += new EventHandler(generalOptionsChanged);

            //Layout options
            viewBox.TextChanged += new EventHandler(layoutOptionsChanged);
            pcViewBox.TextChanged += new EventHandler(layoutOptionsChanged);
            favouritesViewBox.TextChanged += new EventHandler(layoutOptionsChanged);
            showFanArtCheckBox.CheckedChanged += new EventHandler(layoutOptionsChanged);
            fanartDelayBox.ValueChanged += new EventHandler(layoutOptionsChanged);
            showGameArtCheckBox.CheckedChanged += new EventHandler(layoutOptionsChanged);
            gameArtDelayBox.ValueChanged += new EventHandler(layoutOptionsChanged);

            //Stop emu options
            stopEmuCheckBox.CheckedChanged += new EventHandler(stopEmulationOptionsChanged);
            keyMapLabel.TextChanged += new EventHandler(stopEmulationOptionsChanged);

            //Goodmerge options
            txt_7zdll.TextChanged += new EventHandler(goodmergeOptionsChanged);
            goodFiltersTextBox.TextChanged += new EventHandler(goodmergeOptionsChanged);

            //Database options
            autoRefreshGames.CheckedChanged += new EventHandler(databaseOptionsChanged);
            autoImportCheckBox.CheckedChanged += new EventHandler(databaseOptionsChanged);
            exactMatchCheckBox.CheckedChanged += new EventHandler(databaseOptionsChanged);
            approveTopCheckBox.CheckedChanged += new EventHandler(databaseOptionsChanged);
            resizeThumbCheckBox.CheckedChanged += new EventHandler(databaseOptionsChanged);

            //Advanced options
            thumbDirTextBox.TextChanged += new EventHandler(advancedOptionsChanged);
            threadCountUpDown.ValueChanged += new EventHandler(advancedOptionsChanged);
            stopMediaCheckBox.CheckedChanged += new EventHandler(advancedOptionsChanged);
        }

        void advancedOptionsChanged(object sender, EventArgs e)
        {
            updateAdvanced = true;
        }

        void databaseOptionsChanged(object sender, EventArgs e)
        {
            updateDatabase = true;
        }

        void layoutOptionsChanged(object sender, EventArgs e)
        {
            updateLayout = true;
        }

        void generalOptionsChanged(object sender, EventArgs e)
        {
            updateGeneral = true;
        }

        void stopEmulationOptionsChanged(object sender, EventArgs e)
        {
            updateStopEmu = true;
        }

        void goodmergeOptionsChanged(object sender, EventArgs e)
        {
            updateGoodmerge = true;
        }

        #endregion

        void getOptions()
        {
            Options options = Options.Instance;

            shownnameBox.Text = options.GetStringOption("shownname");
            languageBox.Items.AddRange(Translator.getLanguages());
            languageBox.Text = options.GetStringOption("language");
            favouriteslaunchBox.Checked = options.GetBoolOption("startwithfavourites");
            onlyShowPCGamesBox.Checked = options.GetBoolOption("onlyshowpcgames");
            hidelabeldecorationsBox.Checked = options.GetBoolOption("hidelabeldecorations");
            autoconfemuBox.Checked = options.GetBoolOption("autoconfemu");

            viewBox.SelectedIndex = options.GetIntOption("viewemus");
            pcViewBox.SelectedIndex = options.GetIntOption("viewpcgames");
            favouritesViewBox.SelectedIndex = options.GetIntOption("viewfavourites");
            showFanArtCheckBox.Checked = options.GetBoolOption("showfanart");
            fanartDelayBox.Value = options.GetIntOption("fanartdelay");
            showGameArtCheckBox.Checked = options.GetBoolOption("showgameart");
            gameArtDelayBox.Value = options.GetIntOption("gameartdelay");

            stopEmuCheckBox.Checked = options.GetBoolOption("domap");
            keyMapLabel.Text = options.GetStringOption("mappedkey");

            txt_7zdll.Text = options.GetStringOption("7zdllpath");
            goodFiltersTextBox.Text = options.GetStringOption("goodmergefilters");

            autoRefreshGames.Checked = options.GetBoolOption("autorefreshgames");
            autoImportCheckBox.Checked = options.GetBoolOption("autoimportgames");
            exactMatchCheckBox.Checked = options.GetBoolOption("importexact");
            approveTopCheckBox.Checked = options.GetBoolOption("importtop");
            resizeThumbCheckBox.Checked = options.GetBoolOption("resizethumbs");

            thumbDirTextBox.Text = options.GetStringOption("thumblocation");
            stopMediaCheckBox.Checked = options.GetBoolOption("stopmediaplayback");
            //validate threadcount
            int threadCount = options.GetIntOption("importthreadcount");
            if (threadCount > 10)
                threadCount = 10;
            else if (threadCount < 1)
                threadCount = 1;
            threadCountUpDown.Value = threadCount;
        }

        public override void save()
        {
            Options options = Options.Instance;

            if (updateGeneral)
            {
                options.UpdateOption("shownname", shownnameBox.Text);
                options.UpdateOption("language", languageBox.Text);
                options.UpdateOption("startwithfavourites", favouriteslaunchBox.Checked);
                options.UpdateOption("onlyshowpcgames", onlyShowPCGamesBox.Checked);
                options.UpdateOption("hidelabeldecorations", hidelabeldecorationsBox.Checked);
                options.UpdateOption("autoconfemu", autoconfemuBox.Checked);
                updateGeneral = false;
            }

            if (updateLayout)
            {
                options.UpdateOption("viewemus", viewBox.SelectedIndex);
                options.UpdateOption("viewpcgames", pcViewBox.SelectedIndex);
                options.UpdateOption("viewfavourites", favouritesViewBox.SelectedIndex);
                options.UpdateOption("fanartdelay", Convert.ToInt32(fanartDelayBox.Value));
                options.UpdateOption("showfanart", showFanArtCheckBox.Checked);
                options.UpdateOption("gameartdelay", Convert.ToInt32(gameArtDelayBox.Value));
                options.UpdateOption("showgameart", showGameArtCheckBox.Checked);
                updateLayout = false;
            }

            if (updateStopEmu)
            {
                options.UpdateOption("mappedkey", keyMapLabel.Text);
                options.UpdateOption("domap", stopEmuCheckBox.Checked);
                updateStopEmu = false;
            }

            if (updateGoodmerge)
            {
                options.UpdateOption("7zdllpath", txt_7zdll.Text);
                options.UpdateOption("goodmergefilters", goodFiltersTextBox.Text);
                Extractor.Instance.ReInit();
                updateGoodmerge = false;
            }

            if (updateDatabase)
            {
                options.UpdateOption("autorefreshgames", autoRefreshGames.Checked);
                options.UpdateOption("autoimportgames", autoImportCheckBox.Checked);
                options.UpdateOption("importexact", exactMatchCheckBox.Checked);
                options.UpdateOption("importtop", approveTopCheckBox.Checked);
                options.UpdateOption("resizethumbs", resizeThumbCheckBox.Checked);
                updateDatabase = false;
            }

            if (updateAdvanced)
            {
                options.UpdateOption("thumblocation", thumbDirTextBox.Text);
                options.UpdateOption("importthreadcount", (int)threadCountUpDown.Value);
                options.UpdateOption("stopmediaplayback", stopMediaCheckBox.Checked);
                updateAdvanced = false;
            }

            base.save();
        }

        public override void close()
        {
            save();
            base.close();
        }

        private void btnNew7z_Click(object sender, EventArgs e)
        {
            string filter = "7z.dll (7z.dll) | 7z.dll";

            string initialDir;
            int index = txt_7zdll.Text.LastIndexOf("\\");

            if (index > -1)
                initialDir = txt_7zdll.Text.Remove(index);
            else
                initialDir = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);

            using (OpenFileDialog dlg = Conf_GlobalSettings.OpenFileDialog("Select 7z.dll image", filter, initialDir))
            {
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    txt_7zdll.Text = dlg.FileName;
                }
            }
        }

        private void thumbDirButton_Click(object sender, EventArgs e)
        {
            string initialDir = thumbDirTextBox.Text;
            if(!System.IO.Directory.Exists(initialDir))
                initialDir = MediaPortal.Configuration.Config.GetFolder(MediaPortal.Configuration.Config.Dir.Thumbs);

            using (FolderBrowserDialog dlg = Conf_GlobalSettings.OpenFolderDialog("Select thumb directory", initialDir))
            {
                if (dlg.ShowDialog() == DialogResult.OK)
                    thumbDirTextBox.Text = dlg.SelectedPath;
            }
        }

        #region Key Mapping

        bool mapping = false;
        Timer timer = null;

        void mapKeyDown(object o, PreviewKeyDownEventArgs e)
        {
            if (mapping)
            {
                keyMapLabel.Text = Enum.GetName(typeof(Keys), e.KeyCode);
                stopMapping();
            }
        }

        private void mapKeyButton_Click(object sender, EventArgs e)
        {
            keyMapLabel.Focus();
            mapping = true;
            mapKeyButton.Text = "Press Key";
            mapKeyButton.Enabled = false;
            timer = new Timer() { Interval = 5000 };
            timer.Tick += new EventHandler(delegate(object o, EventArgs ev)
            {
                stopMapping();
            }
                );
            timer.Start();
        }

        private void stopMapping()
        {
            mapping = false;
            mapKeyButton.Text = "Map";
            mapKeyButton.Enabled = true;
            if (timer != null)
            {
                timer.Stop();
                timer.Dispose();
                timer = null;
            }
        }

        #endregion

        private void ignoredFilesButton_Click(object sender, EventArgs e)
        {
            using (Conf_IgnoredFiles dlg = new Conf_IgnoredFiles())
                dlg.ShowDialog();
        }

    }
}
